<html>
<head>
<!--

Code by Simon East
Version 2.0
http://code.google.com/p/google-bulk-geocoder/
Copyright 2011

 	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.


Note: Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day

-->
<title>Bulk Geocoding Tool, using Google Maps Api v3</title>
</head>
<body>

<form>
	<h2>Bulk Geocoder</h2>
	<i>Important: the coordinates returned from this tool are only to be used in conjuction with Google Maps</i><br>
	Enter list of addresses on separate lines:
	<br>
	<textarea cols=80 rows=12></textarea>
	<br>Note: you have a limit of 2,500 geocoding requests per day (per IP address)<br>
	<input type="submit" value="Go and Geocode!">
	<input type="button" value="Stop!" id="stopBtn"/>
	<input type="reset">
	


<div id="resultBox">
	<textarea cols=80 rows=12></textarea><textarea cols=40 rows=12></textarea>
</div>
<input type="text" size="5" id="txtCount" value="0"/>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
 <script src="http://maps.google.com/maps/api/js?&amp;sensor=false&amp;key=ABQIAAAAnclR83ppqH3dsaTFAo-k7BT42jgS1xfaYFXGX_QlHDCtV0nt4xRzFloTa-gTb46aT_E2aF9vsWjopg" type="text/javascript"></script>
    
<script type="text/javascript">

	var t = $('#resultBox textarea')[0];
	var e = $('#resultBox textarea')[1];
	var txtCount = $('#txtCount');
	//var maxConcurrentRequests = 1;
	var delayTime = 620;
	g = new google.maps.Geocoder();

	$('form').submit(function(e) {
		e.preventDefault();
		stop = false;
		// store list into an array queue
		queue = $('textarea').val().trim().split('\n');
		finalResults = queue.slice(0);	// create a copy of our array
		nextRequestInQueue();
	});
	
	$('#stopBtn').click(function(){ stop = TRUE })
	
	function nextRequestInQueue() {
		
		if (queue.length) {
		
			var item = queue.shift();
			console.log('Now requesting ' + item);
			
			g.geocode( { 'address': item, 'region': 'au'}, function (results, status) {
				  txtCount.val(txtCount.val() - (-1))				// negative forces numerical addition
				  if (status == google.maps.GeocoderStatus.OK) {
					//console.log('Result for ' + item);
					//console.log(results);
					// Append result to second textarea
					finalResults[finalResults.indexOf(item)] = item + '\t' + results[0].geometry.location.lng() + '\t' 
								+ results[0].geometry.location.lat();
					t.value = finalResults.join('\n');
				  } else {
					//console.warn("Geocode was not successful for the following reason: " + status + ' > ' + item);
					if (status == "OVER_QUERY_LIMIT") {
						delayTime *= 1.08;	// let's go a little slower
						queue.push(item);	// and retry the item again later
					} else {
						e.value += item + '\n';  // if it was a DIFFERENT error, print it to side dialog box
					}
				  }
				if (!stop) setTimeout(nextRequestInQueue, delayTime);
			});
			
		}
	}
	

</script>
</body>
</html>